package com.tb.education.common.packets.request;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.HexUtil;
import com.tb.education.common.enumeration.OperationTypeEnum;
import com.tb.education.common.enumeration.PacketTypeEnum;
import com.tb.education.common.packets.BaseBody;
import com.tb.education.common.packets.Head;
import com.tb.education.common.packets.Msg;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备登录-消息体
 *
 * @author wangze
 * @date 2020-07-08 16:24.
 **/
public class DeviceLoginRequestBody extends BaseBody {

    /**
     * 终端普通键数量(如值是5则表示终端有5个按键，
     * 序号为1-5)
     */
    private Integer keyNumbers;

    /**
     * 终端有没有SOS键(0=没有, 1=有)
     */
    private Integer hasSos;

    /**
     * 终端类型(1=GPS, 2=CellID, 3=AGPS)
     */
    private Integer deviceType;

    /**
     * 终端是否支持区域报警终端侧解决方案功能(0=
     * 不具备, 1=具备)
     */
    private Integer hasAreaAlert;


    /**
     * 终端是否具备设置呼入号码功能(0=不具备, 1=具 备)
     */
    private Integer hasDialNumber;


    /**
     * 终端软件协议版本号,一共2位,第一位为大版本
     * 号,第二位为小版本号,目前该字段值为21，小版本号
     * 为0为每一版本的基础版本
     */
    private Integer version;


    @Override
    public String toString() {
        List<Integer> list = new ArrayList<>();
        list.add(keyNumbers);
        list.add(hasSos);
        list.add(deviceType);
        list.add(hasAreaAlert);
        list.add(hasDialNumber);
        list.add(version);
        return String.join(Msg.SEPARATOR, list.stream().map(x -> x.toString()).collect(Collectors.toList()));

    }

    public Integer getKeyNumbers() {
        return keyNumbers;
    }

    public void setKeyNumbers(Integer keyNumbers) {
        this.keyNumbers = keyNumbers;
    }

    public Integer getHasSos() {
        return hasSos;
    }

    public void setHasSos(Integer hasSos) {
        this.hasSos = hasSos;
    }

    public Integer getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(Integer deviceType) {
        this.deviceType = deviceType;
    }

    public Integer getHasAreaAlert() {
        return hasAreaAlert;
    }

    public void setHasAreaAlert(Integer hasAreaAlert) {
        this.hasAreaAlert = hasAreaAlert;
    }

    public Integer getHasDialNumber() {
        return hasDialNumber;
    }

    public void setHasDialNumber(Integer hasDialNumber) {
        this.hasDialNumber = hasDialNumber;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * [35380100360174,898600MFSSYYGXXXXXXP,201805082000001000,DEVICE_LOGIN,
     * 3,12,5@1@1@0@1@21]
     * @param args
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        Msg<TemperRequest> msg = new Msg<TemperRequest>();
//        DeviceLoginRequestBody request =new DeviceLoginRequestBody();
//        request.setKeyNumbers(5);
//        request.setHasSos(1);
//        request.setDeviceType(1);
//        request.setHasAreaAlert(0);
//        request.setHasDialNumber(1);
//        request.setVersion(21);

        TemperRequest request=new TemperRequest();
        request.setTemperature("38.5");
        Head headPacket=new Head();
        headPacket.setDeviceNo("35380100360174");
        headPacket.setIccId("898600MFSSYYGXXXXXXP12345");
        headPacket.setSerialNo(DateUtil.format(new Date(),"yyyyMMddHHmmss")+'0');
        //headPacket.setOperationType(OperationTypeEnum.DEVICE_LOGIN.getKey());
        headPacket.setOperationType(OperationTypeEnum.REPORT_TEMPER.getKey());
        headPacket.setTime(LocalDateTime.now());
        headPacket.setPacketLength(request.toString().length());
        headPacket.setPacketType(PacketTypeEnum.CLIENT_TO_SERVER_REQUEST);
        msg.setBody(request);
        msg.setHead(headPacket);

        System.out.println(msg.toString());

        System.out.println(HexUtil.encodeHex(msg.toString(), Charset.forName("utf-8")));


    }

}
